package util;

import net.sf.json.JSONObject;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author fzh
 * @since 2017/10/29
 */
public class RequestUtil {
    public static JSONObject getJsonFromRequest(HttpServletRequest request) {
        try {
            ServletInputStream inputStream = request.getInputStream();
            BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder tStringBuffer = new StringBuilder();
            String sTempOneLine;
            while ((sTempOneLine = tBufferedReader.readLine()) != null) {
                tStringBuffer.append(sTempOneLine);
            }
            String jsonString;
            jsonString = new String(tStringBuffer.toString().getBytes());
            return JSONObject.fromObject(jsonString);
        } catch (IOException e) {
            return null;
        }
    }
}
